#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0, right = 0;
        int n = nums.size();
        int len = 0;
        int sum = nums[0];
        while (right < n)
        {
            if (sum >= target)
            {
                if (len == 0 || len > right - left + 1)
                    len = right - left + 1;
                if (len == 1)
                {
                    return len;
                }
                else
                {
                    sum -= nums[left];
                    left++;
                }
            }
            else
            {
                if (right < n - 1)
                    sum += nums[++right];
                else
                    break;
            }
        }
        return len;
    }
};